﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;

namespace ThisIsTheOnlyChamber
{
    class Menu : GameState
    {
        private string[] menuItems = new string[3];
        private string[] developers = new string[5];

        int currentItem = 0;
        bool inCredits = false;

        SpriteBatch sb;
        SpriteFont sf;
        Texture2D menuBackground;

        SoundEffect flipSound;
        SoundEffect selectSound;

        GamePadState lastState;
        KeyboardState lastKeyboardState;

        public Menu(Game g)
            : base( g )
        {
            menuItems[0] = "Start Game";
            menuItems[1] = "Credits";
            menuItems[2] = "Quit";

            developers[0] = "Archbishop Bradley Weinberger";
            developers[1] = "Cardinal Samuel Fansler";
            developers[2] = "Father Andrew Briggs";
            developers[3] = "Reverand Andrew Allen";
            developers[4] = "Pope Benedict XII";
        }

        private void ProcessItem()
        {
            if (!inCredits)
            {
                switch (currentItem)
                {
                    case 0:
                        PublishMessage(new Message(Message.Type.StartGame));
                        break;
                    case 1:
                        inCredits = true;
                        break;
                    case 2:
                        PublishMessage(new Message(Message.Type.Quit));
                        break;
                    default:
                        break;
                }
            }
            else
            {
                inCredits = false;
            }
        }

        public override void ProcessGamePad(GamePadState gs)
        {
            if (!inCredits && gs.DPad.Up == ButtonState.Pressed && currentItem > 0 && ( lastState == null || lastState.DPad.Up != ButtonState.Pressed))
            {
                flipSound.Play();
                currentItem--;
            }
            else if (!inCredits && gs.DPad.Down == ButtonState.Pressed && currentItem < menuItems.Length - 1 && (lastState == null || lastState.DPad.Down != ButtonState.Pressed))
            {
                flipSound.Play();
                currentItem++;
            }
            else if (gs.IsButtonDown(Buttons.A) && (lastState == null || !lastState.IsButtonDown(Buttons.A)))
            {
                selectSound.Play();
                ProcessItem();
            }

            lastState = gs;
        }

        public override void ProcessKeyboardMouse(KeyboardState ks, MouseState ms)
        {
            if (!inCredits && ks.IsKeyDown(Keys.Up) && currentItem > 0 && (lastKeyboardState == null || lastKeyboardState.IsKeyUp(Keys.Up)))
            {
                flipSound.Play();
                currentItem--;
            }
            else if (!inCredits && ks.IsKeyDown(Keys.Down) && currentItem < (menuItems.Length - 1) && (lastKeyboardState == null || lastKeyboardState.IsKeyUp(Keys.Down)))
            {
                flipSound.Play();
                currentItem++;
            }
            else if (ks.IsKeyDown(Keys.Enter) && (lastKeyboardState == null || lastKeyboardState.IsKeyUp(Keys.Enter)))
            {
                selectSound.Play();
                ProcessItem();
            }

            lastKeyboardState = ks;
        }

        protected override void LoadContent()
        {
            //Graphics
            sb = new SpriteBatch(Game.GraphicsDevice);
            sf = Game.Content.Load<SpriteFont>("MenuFont");
            menuBackground = Game.Content.Load<Texture2D>("MenuBackground");

            //Sounds
            flipSound = Game.Content.Load<SoundEffect>("flip");
            selectSound = Game.Content.Load<SoundEffect>("select");
        }

        public override void Update(GameTime gameTime)
        {

        }

        public override void Draw(GameTime gameTime)
        {
            float verPosition = 150f, horPosition = 100f, alphaValue = 1f;

            sb.Begin();

            sb.Draw(menuBackground, new Vector2(), null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0f);
            sb.DrawString(sf, "This Is The Only Chamber", new Vector2(50, 100), Color.White, 0, Vector2.Zero, 1.5f, SpriteEffects.None, 0);

            if (!inCredits)
            {
                for (int i = 0; i < menuItems.Length; i++)
                {
                    Vector2 itemPostition = new Vector2(horPosition, verPosition);
                    Color itemColor = Color.White;

                    if (i == currentItem)
                        itemColor = new Color(new Vector4(1, 0, 0, alphaValue));
                    else
                        itemColor = new Color(new Vector4(1, 1, 1, alphaValue));

                    sb.DrawString(sf, menuItems[i], itemPostition, itemColor, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
                    verPosition += 30;
                }
            }
            else
            {
                for (int i = 0; i < developers.Length; i++)
                {
                    Vector2 itemPostition = new Vector2(horPosition, verPosition);
                    Color itemColor = itemColor = new Color(new Vector4(1, 1, 1, alphaValue));

                    sb.DrawString(sf, developers[i], itemPostition, itemColor, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
                    verPosition += 30;
                }

                verPosition += 30;
                Vector2 ip = new Vector2(horPosition, verPosition);
                sb.DrawString(sf, "Back", ip, new Color(new Vector4(1, 0, 0, alphaValue)), 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            }

            sb.End();

            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
        }
    }
}
